import router from '~/router'
import { Modal } from 'ant-design-vue';
import { createVNode } from 'vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';


const showConfirm = (afterCallback, closeCallback) => {
  Modal.confirm({
    title: '退出',
    icon: createVNode(ExclamationCircleOutlined),
    content: createVNode('div', { style: 'color:000;' }, '您好， 确定要退出公共建筑能耗管控平台吗？'),
    onOk() {
      afterCallback && afterCallback();
    },
    onCancel() {
      console.log('Cancel');
      closeCallback && closeCallback()
    },
    class: 'test',
  });
};

const toLogin = () => {

}


router.beforeEach(async (to, _, next) => {
  // 获取
  const userStore = useUserStore()
  const quitType = to.path == '/quit';
  const loginType = sessionStorage.getItem('loginType');
  const quitCallback = () => {
    sessionStorage.removeItem('loginType');
    window.location.href = '/'
  }
  quitType && showConfirm(quitCallback, () => {
    next(_.path)
  });
  if (!loginType && to.path !== '/login') return next('/login')
  if (!userStore.routerData) {
    const currentRoute = await userStore.generateDynamicRoutes();
    await router.addRoute(currentRoute)
    next({
      ...to,
      replace: true,
    })
    return
  }
  return next();
})

router.afterEach((to) => {
  // TODO
  const title = to.meta?.title
  if (title)
    document.title = title as string
})
